﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ATM.DBlayer
{

    public class PayBillDB
    {
        public static DataGridView data = new DataGridView();
        public int check1;
        string comp;
        string accountnum2;
       public float num, num2;
        public float result;
        float zerobalance = 0;

        public void showbillDB(string company, string accountnum)
        {
            comp = company;
            accountnum2 = accountnum;
            string myconnection = "datasource=localhost;port=3306;username=root;password=1234";
            MySqlConnection myconn = new MySqlConnection(myconnection);

            MySqlCommand SelectCommand = new MySqlCommand("SELECT * FROM atm.companies where company='" + this.comp + "' AND Account_Number='" + this.accountnum2 + "';", myconn);

            try
            {

                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = SelectCommand;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bsource = new BindingSource();
                bsource.DataSource = dbdataset;
                data.DataSource = bsource;
                sda.Update(dbdataset);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void paybillDB(string company, string accountnum)
        {

            {
                accountnum2 = accountnum;
                comp = company;

             
                try
                {
                    string myconnection1 = "datasource=localhost;port=3306;username=root;password=1234";
                    MySqlConnection myconn1 = new MySqlConnection(myconnection1);
                    MySqlCommand SelectCommand = new MySqlCommand("SELECT * FROM atm.companies where company='" + this.comp + "' AND Account_Number='" + this.accountnum2 + "';", myconn1);
                    MySqlDataReader myreader;

                    myconn1.Open();
                    myreader = SelectCommand.ExecuteReader();
                    while (myreader.Read())//law 3amal coonection w3amel select fe3lan 5osh gwa el while
                    {
                        num2 = float.Parse((myreader["bill_balance"].ToString()));//hat el balance mn eldatabase w7awelo fl float w7oto fe num
                    }


                    myconn1.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }




                try
                {
                    string myconnection = "datasource=localhost;port=3306;username=root;password=1234";
                    MySqlConnection myconn = new MySqlConnection(myconnection);
                    MySqlCommand SelectCommand = new MySqlCommand("SELECT * FROM atm.atm_customers where Account_Number='" + this.accountnum2 + "';", myconn);
                    MySqlDataReader myreader;

                    myconn.Open();
                    myreader = SelectCommand.ExecuteReader();
                    while (myreader.Read())//law 3amal coonection w3amel select fe3lan 5osh gwa el while
                    {
                        num = float.Parse((myreader["balance"].ToString()));//hat el balance mn eldatabase w7awelo fl float w7oto fe num
                    }



                    myconn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

             
                    try
                    {
                        {
                            string myconnection3 = "datasource=localhost;port=3306;username=root;password=1234";
                            MySqlConnection myconn3 = new MySqlConnection(myconnection3);

                            MySqlCommand SelectCommand3 = new MySqlCommand("update atm.companies set bill_balance='" + this.zerobalance + "' where company='" + this.comp + "'AND Account_Number='" + this.accountnum2 + "';", myconn3);
                            // hna bafta7 eldatabase wba7ot feha elnateq elnha2y elbalance el2asly + el mabla3' elly zawedo elly homa 3la ba3d ma7toteen fe result
                            MySqlDataReader myreader;

                            myconn3.Open();
                            myreader = SelectCommand3.ExecuteReader();



                            myconn3.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                


            }
        }


    }


}
